Skip to content

Fix MA0042 false positive when non-generic method has a same-named generic overload with awaitable return type#1102

Merged
meziantou merged 2 commits into
mainfrom
copilot/fix-false-positive-ma0042
Apr 24, 2026
Merged

Fix MA0042 false positive when non-generic method has a same-named generic overload with awaitable return type#1102
meziantou merged 2 commits into
mainfrom
copilot/fix-false-positive-ma0042

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 24, 2026

MA0042 incorrectly reports a diagnostic when a non-generic method (e.g., TUnit's Assert.That(string?)) coexists with a same-named generic overload (e.g., Assert.That<T>(T)) that returns an awaitable type. The suggested fix (await Assert.That(x)) fails to compile because C# overload resolution always prefers non-generic methods — the call still resolves to the non-awaitable non-generic overload.

Changes

  • DoNotUseBlockingCallInAsyncContextAnalyzer.cs: In FindPotentialAsyncEquivalent, skip generic candidates when performing a same-name search (methodName == targetMethod.Name) and the original method is non-generic. Since the compiler will always prefer the non-generic overload at the call site, the generic awaitable variant can never actually be selected by applying await.

  • DoNotUseBlockingCallInAsyncContextAnalyzer_AsyncContextTests.cs: Regression test covering the pattern below.

static class Assert
{
    public static ValueAssertion  That(string? value) => ...;   // non-awaitable
    public static ValueAssertion<T> That<T>(T value) => ...;    // awaitable
}

// MA0042 was incorrectly flagged here — the fix would still call That(string?), not That<T>
await Assert.That(x).IsEqualTo("Blah");

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • st8vsblobprodcus339.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/Meziantou.Analyzer.slnx --packages /tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/Meziantou.Analyzer.Test.csproj --packages /tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI linked an issue Apr 24, 2026 that may be closed by this pull request
…le same-named overload

Agent-Logs-Url: https://github.com/meziantou/Meziantou.Analyzer/sessions/771fa8ce-1473-48ab-acdf-f27908b008c1

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix false positive of MA0042 in version 3.0.41 Fix MA0042 false positive when non-generic method has a same-named generic overload with awaitable return type Apr 24, 2026
Copilot AI requested a review from meziantou April 24, 2026 13:03
@meziantou meziantou marked this pull request as ready for review April 24, 2026 20:37
@meziantou meziantou merged commit a42144e into main Apr 24, 2026
13 checks passed
@meziantou meziantou deleted the copilot/fix-false-positive-ma0042 branch April 24, 2026 20:37
This was referenced Apr 24, 2026
This was referenced May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3.0.41 introduces false positive of MA0042

2 participants